package com.xy.modular.basics.action;

import com.xy.framework.base.controller.BaseController;
import com.xy.framework.mreturn.MReturnObject;
import com.xy.framework.mreturn.MReturnUntil;
import com.xy.framework.pager.PageRequest;
import com.xy.framework.pager.SearchCondition;
import com.xy.framework.util.uuid.UUIDUtils;
import com.xy.modular.basics.entity.Dictionary;
import com.xy.modular.basics.service.IDictionaryService;
import com.xy.modular.sys.action.ModuleController;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/dic")
public class DictionaryController extends BaseController {
	static Logger log = LoggerFactory.getLogger(ModuleController.class);

	@Resource
	private IDictionaryService dictionaryService;

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public @ResponseBody MReturnObject list(Dictionary dictionary, HttpServletRequest request) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.addAndCondition(Dictionary.PARENT, SearchCondition.EQUAL, dictionary.getParent());
		return new MReturnObject(MReturnObject.SUCCESS, dictionaryService.findColumnsPageAll(pageRequest),
				dictionaryService.getCount(pageRequest));
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/treeDic", method = RequestMethod.GET)
	public @ResponseBody MReturnObject treeModule(HttpServletRequest request,Dictionary dictionary) {
		PageRequest pageRequest = newPageRequest(request);
		pageRequest.addAndCondition(Dictionary.PARENT, SearchCondition.EQUAL, dictionary.getParent()==null?"0":dictionary.getParent());
		return new MReturnObject(MReturnObject.SUCCESS, dictionaryService.findAll(pageRequest));
	}
	
	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody MReturnObject save(Dictionary dictionary) {
		try {
			dictionaryService.validateEntity(dictionary);
			dictionary.setId(UUIDUtils.UUID());
			dictionaryService.save(dictionary);
		} catch (Exception e) {
			log.info("菜单保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS, dictionaryService.findAll());
	}

	@RequiresRoles("SYSTEMER")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody MReturnObject update(Dictionary dictionary) {
		try {
			dictionaryService.validateEntity(dictionary);
			dictionaryService.save(dictionary);
		} catch (Exception e) {
			log.info("菜单保存失败", e);
			return new MReturnObject(MReturnObject.SUCCESS, MReturnUntil.ERROR_MOUDLE_SAVE);
		}
		return new MReturnObject(MReturnObject.SUCCESS, dictionaryService.findAll());
	}
}
